package com.logica.mdm.webservices.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.logica.mdm.webservices.dao.SearchPartyRelDao;
import com.logica.mdm.webservices.dao.beans.PartyRelResultBean;

/**
 * Implementation using NamedParameterJdbcTemplate
 */
@Repository
public class JdbcSearchPartyRelDaoImpl implements SearchPartyRelDao {

	private NamedParameterJdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
	    this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public List<PartyRelResultBean> searchPartyRelByParty(String sqlQuery, Map<String,?> sqlParams) {
		return this.jdbcTemplate.query(sqlQuery, sqlParams, new PartyRelMapper());
	}
	
	/**
	 * Inner class defining the mapping between the database and PartyRelResultBean
	 */
	private static final class PartyRelMapper implements RowMapper<PartyRelResultBean> {
		public PartyRelResultBean mapRow(ResultSet rs, int rowNum) throws SQLException {
			PartyRelResultBean partyRel = new PartyRelResultBean();
			partyRel.setRowidObject(rs.getString("ROWID_OBJECT"));
			partyRel.setRowidRelType(rs.getString("ROWID_REL_TYPE"));
			partyRel.setPartyId1(rs.getString("PARTY_ID1"));
			partyRel.setPartyId2(rs.getString("PARTY_ID2"));
			partyRel.setRelName(rs.getString("REL_NAME"));
			partyRel.setRelDesc(rs.getString("REL_DESC"));
			partyRel.setOcodAccountNumber(rs.getString("OCOD_ACCOUNT_NUMBER"));
			return partyRel;
		}
	}
}
